package com.example.controll.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.R;
import com.example.domain.childer_articles;
import com.example.domain.lease_review;
import com.example.domain.news;
import com.example.service.article.articlesService;
import com.example.service.img.imgService;
import com.example.service.news.newService;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/articles")
public class articleControll {

    @Resource
    private articlesService articlesService;
    @Resource
    private com.example.service.img.imgService imgService;

    @Resource
    private com.example.service.news.newService newService;


    @RequestMapping("/category")
    private R findBycategory(int category){
        return new R(true,articlesService.findBycategory(category));
    }
    @RequestMapping("/name")
    private R findBycategory(childer_articles articles, int currentPage, int pageSize){


//        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = articlesService.getPage((int)page.getPages(), pageSize,articles);
        }

//        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);

        return new R(true,page);
    }

    @RequestMapping("/name1")
    private R findByname(childer_articles articles, int currentPage, int pageSize){

//        getPage(currentPage, pageSize,articles);
//        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        IPage<childer_articles> page = articlesService.getPagebyname(currentPage,pageSize,articles);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = articlesService.getPagebyname((int)page.getPages(), pageSize,articles);
        }

//        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);

        return new R(true,page);
    }

    @RequestMapping("/type")
    private R findBytype(childer_articles articles, int currentPage, int pageSize){

        System.out.println("ccccc");
//        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = articlesService.getPage((int)page.getPages(), pageSize,articles);
        }

//        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);

        return new R(true,page);
    }

    @RequestMapping("/top")
    private R selectTop(){
        return new R(true,articlesService.selectTop());
    }


    @RequestMapping("/id")
    private R selectbyid(int id){
        return new R(true,articlesService.selectbyid(id));
    }


    @RequestMapping("/userid")
    private R selectbyuserid(String userid){
        return new R(true,articlesService.selectbyuserid(userid));
    }

    @PostMapping("/selectall")
    private R selectall(Integer a,Integer b){
        System.out.println(a);
        System.out.println(b);
        return new R(true,articlesService.selectall(a,b));
    }
//    数据总数
    @RequestMapping("/number")
    private R selectnumberall(){
        return new R(true,articlesService.allnumber());
    }

//    按id删除儿童用品
    @RequestMapping("/articlesid")
    private R deletebyid(int id){
        boolean deletebyid = articlesService.deletebyid(id);
        return new R(deletebyid,deletebyid?"删除成功":"删除失败");
    }

//    审核通过后插入数据
    @RequestMapping("/insertbyid")
    private R insertarticle(int id){
        lease_review review = imgService.selectBYid(id);
        news news1 = new news();
        news1.setXiaoxi("您的儿童用品已经通过审核");
        news1.setUserid(review.getUserid());
        news1.setType("平台");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(simpleDateFormat.format(date));
        String format = simpleDateFormat.format(date);
        news1.setTime(format);
        newService.insertnews(news1);

        com.example.domain.childer_articles childer_articles = new childer_articles();
        BeanUtils.copyProperties(review,childer_articles);
        childer_articles.setId(0);
        System.out.println(childer_articles);
        boolean insert = articlesService.insert(childer_articles);
        imgService.deletelease_review(review.getId());
        return new R(insert,insert?"添加成功":"添加失败");

    }


}
